package com.nervenets.utils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class RequestUtils {
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (null != ip && !"".equals(ip.trim())
                && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
        ip = request.getHeader("X-Forwarded-For");
        if (null != ip && !"".equals(ip.trim())
                && !"unknown".equalsIgnoreCase(ip)) {
            // get first ip from proxy ip
            int index = ip.indexOf(',');
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        return request.getRemoteAddr();
    }

    // public static String getClientIpAddr(HttpServletRequest request) {
    // String ip = request.getHeader("x-forwarded-for");
    // if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    // ip = request.getHeader("Proxy-Client-IP");
    // }
    // if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    // ip = request.getHeader("WL-Proxy-Client-IP");
    // }
    // if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    // ip = request.getRemoteAddr();
    // if (ip.equals("127.0.0.1")) {
    // // 根据网卡取本机配置的IP
    // InetAddress inet = null;
    // try {
    // inet = InetAddress.getLocalHost();
    // } catch (UnknownHostException e) {
    // e.printStackTrace();
    // }
    // ip = inet.getHostAddress();
    // }
    // }
    // // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
    // if (ip != null && ip.length() > 15) {
    // if (ip.indexOf(",") > 0) {
    // ip = ip.substring(0, ip.indexOf(","));
    // }
    // }
    // return ip;
    // }

    public static String getClientIpAddr(HttpServletRequest httpservletrequest) {
        if (httpservletrequest == null)
            return null;
        // System.err
        // .println("--------------------------------------------------------------------------------------------------------------------------------------------");
        // System.err.println("X-Forwarded-For:"
        // + httpservletrequest.getHeader("X-Forwarded-For"));
        // System.err
        // .println("--------------------------------------------------------------------------------------------------------------------------------------------");
        // System.err.println("Proxy-Client-IP:"
        // + httpservletrequest.getHeader("Proxy-Client-IP"));
        // System.err
        // .println("--------------------------------------------------------------------------------------------------------------------------------------------");
        // System.err.println("WL-Proxy-Client-IP:"
        // + httpservletrequest.getHeader("WL-Proxy-Client-IP"));
        // System.err
        // .println("--------------------------------------------------------------------------------------------------------------------------------------------");
        // System.err.println("HTTP_CLIENT_IP:"
        // + httpservletrequest.getHeader("HTTP_CLIENT_IP"));
        // System.err
        // .println("--------------------------------------------------------------------------------------------------------------------------------------------");
        // System.err.println("HTTP_X_FORWARDED_FOR:"
        // + httpservletrequest.getHeader("HTTP_X_FORWARDED_FOR"));
        // Enumeration enu = httpservletrequest.getHeaderNames();// 取得全部头信息
        // while (enu.hasMoreElements()) {// 以此取出头信息
        // String headerName = (String) enu.nextElement();
        // String headerValue = httpservletrequest.getHeader(headerName);
        // System.err.println(headerName + ":" + headerValue);
        // }
        System.err.println("REMOTE_IP:"
                + httpservletrequest.getRemoteAddr());
        // for (String name : httpservletrequest.getHeaderNames()) {
        //
        // }
        String s = httpservletrequest.getHeader("X-Forwarded-For");
        if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
            s = httpservletrequest.getHeader("Proxy-Client-IP");
        if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
            s = httpservletrequest.getHeader("WL-Proxy-Client-IP");
        if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
            s = httpservletrequest.getHeader("HTTP_CLIENT_IP");
        if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
            s = httpservletrequest.getHeader("HTTP_X_FORWARDED_FOR");
        if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
            s = httpservletrequest.getRemoteAddr();
        if ("127.0.0.1".equals(s) || "0:0:0:0:0:0:0:1".equals(s))
            try {
                s = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException unknownhostexception) {
            }
        return s;
    }
}
